package com.das.mechanic_base.adapter.maintain;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.greendao.AloneServiceNewBean;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3AloneServiceSecondAdapter extends RecyclerView.Adapter<X3AloneServiceSecondAdapter.AloneServiceHolder> {

    private List<AloneServiceNewBean.DetectionTouchServiceEntityListBean> mList;

    private Context mContext;
    private final String km;
    private int afterDay;
    private long questionBaseId;

    public X3AloneServiceSecondAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();
        km = (String) SpHelper.getData(X3Constent.MILEAGEUNIT, "km");
    }

    public void setData(List<AloneServiceNewBean.DetectionTouchServiceEntityListBean> mList, long questionBaseId){
        this.mList = mList;
        this.questionBaseId = questionBaseId;
        notifyDataSetChanged();
    }

    public void delItem(int position){
        mList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position,mList.size() + 1 - position);
    }

    @NonNull
    @Override
    public AloneServiceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AloneServiceHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_alone_service_system_item_second,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull AloneServiceHolder holder, int position) {
        holder.tv_name.setText(mList.get(position).touchServiceBaseName);
        X3GlideNewUtils.loadRoundOriginCornerImage(mContext, X3StringUtils.getImageUrl(mList.get(position).serviceBaseImgUrl),holder.iv_img, X3ScreenUtils.dipToPx(6,mContext), R.mipmap.x3_service_normal_bg);
//        if(mList.get(position).warningDate == null){
//            holder.tv_time.setText(DateUtils.getShowTime(mList.get(position).mgtCreate));
//        }else {
//            holder.tv_time.setText(DateUtils.getShowTime(mList.get(position).warningDate));
//        }

//        holder.tv_mile.setText(String.format(mContext.getString(R.string.expect_mile),mList.get(position).recommendSafeMiles+km));


            int nextReplaceCycleKm = X3StringUtils.getInt(mList.get(position).recommendSafeMiles);
            if(mList.get(position).perMiles == 0.0 || mList.get(position).perMiles ==0.00){
                afterDay = 0;
            }else {
                afterDay = X3StringUtils.getInt((nextReplaceCycleKm - mList.get(position).currentMiles) / mList.get(position).perMiles);
            }

            if (afterDay <= 0) {

                holder.tv_time.setTextColor(Color.parseColor("#F64458"));
                holder.tv_time.setText(mContext.getString(R.string.x3_now_to_work));
            }else {

                holder.tv_time.setTextColor(Color.parseColor("#707380"));
                String remildType = mList.get(position).reductionType;
                if ("TIME".equals(remildType)) {
                    // 按保养总时间
                    String warningDate = mList.get(position).warningDate;
                    String showAloneTime = X3DateUtils.getShowAloneTime(warningDate);
                    nextReplaceCycleKm = X3StringUtils.getInt(mList.get(position).perMiles * afterDay + mList.get(position).currentMiles);
                    holder.tv_mile.setText(String.format(mContext.getString(R.string.expect_mile),nextReplaceCycleKm + km));
                    holder.tv_time.setText(showAloneTime);
                }else {
                    // 按保养总里程
                    String addDaysMonth = X3DateUtils.getAddDaysMonth(afterDay);
                    holder.tv_time.setText(String.format(mContext.getString(R.string.expect_mile),nextReplaceCycleKm + km));
                    holder.tv_mile.setText(addDaysMonth);
                }
            }

            holder.iv_delete.setOnClickListener(v -> {
                if(iOnClickSecondSer != null){
                    iOnClickSecondSer.iOnClickSecondSerDel(mList.get(position),questionBaseId,position);
                }
            });



        holder.itemView.setOnClickListener(v -> {
            if(iOnClickSecondSer != null){
                iOnClickSecondSer.iOnClickItem(mList.get(position),questionBaseId);
            }
        });
    }


    IOnClickSecondSer iOnClickSecondSer;

    public void setiOnClickModify(IOnClickSecondSer iOnClickSecondSer) {
        this.iOnClickSecondSer = iOnClickSecondSer;
    }

    public interface IOnClickSecondSer {
        void iOnClickSecondSerDel(AloneServiceNewBean.DetectionTouchServiceEntityListBean detectionTouchServiceEntityListBean, long questionBaseId,int position);
        void iOnClickItem(AloneServiceNewBean.DetectionTouchServiceEntityListBean bean,long questionBaseId);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class AloneServiceHolder extends RecyclerView.ViewHolder {
        ImageView iv_img;
        TextView tv_name;
        TextView tv_mile;
        ImageView iv_delete;
        TextView tv_time;
        public AloneServiceHolder(@NonNull View itemView) {
            super(itemView);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_mile = itemView.findViewById(R.id.tv_mile);
            iv_delete = itemView.findViewById(R.id.iv_delete);
            tv_time = itemView.findViewById(R.id.tv_time);
        }
    }

}
